<template>
  <el-drawer v-model="dialogVisible" title="数据更新" size="600px">
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      :rules="rules"
      label-width="120px"
      class="demo-ruleForm2"
    >
      <el-alert
        title="数据更新存在一定滞后性，我们推荐在查看某段时期的数据前先对该时期数据进行更新"
        type="success"
        :closable="false"
      />
      <br />
      <el-form-item label="日期：" prop="date">
        <el-date-picker
          v-model="ruleForm.date"
          type="monthrange"
          range-separator="至"
          start-placeholder="起始时间"
          end-placeholder="结束时间"
          format="YYYY-MM"
          value-format="YYYY-MM"
        />
      </el-form-item>
      <el-form-item>
        <el-checkbox v-model="checked" label="全部" size="large" />
      </el-form-item>
      <el-form-item label="站点名称：" prop="siteId" v-if="!checked">
        <el-select
          v-model="ruleForm.siteId"
          placeholder="请选择站点名称"
          style="width: 100%"
          filterable
          @change="getSiteInfo"
        >
          <el-option
            v-for="ite in siteIdList"
            :key="ite.id"
            :label="ite.siteName"
            :value="ite.id"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <el-row :gutter="20" style="margin: 0" v-if="ruleForm.siteId && !checked">
      <el-col :span="item.span" style="padding: 0" v-for="item in column" :key="item">
        <div class="col-content">
          <div class="label-info"
            ><el-icon class="icons"><component :is="item.icon" /></el-icon>{{ item.label }}</div
          >
          <div class="content-info">
            <div v-if="item.itemIf">
              <el-tag effect="dark" :type="baseInfo[item.key] ? 'success' : 'danger'">
                {{ baseInfo[item.key] ? '启用' : '禁用' }}
              </el-tag>
            </div>
            <div v-else> {{ baseInfo[item.key] }}&nbsp;{{ item.unit }} </div>
          </div>
        </div>
      </el-col>
    </el-row>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submit(ruleFormRef)">确定</el-button>
      </span>
    </template>
  </el-drawer>
</template>

<script lang="ts" setup>
  import { ref, reactive, nextTick } from 'vue'
  import type { FormInstance } from 'element-plus'
  import { postSiteDetailCalculate } from '@/api/controller/site-detail-controller/index'
  import { postSiteMatch, getSiteById } from '@/api/controller/site-controller/index'
  import { ElMessage } from 'element-plus'
  import { ifWaiting } from '@/utils/waiting'
  const ruleFormRef = ref<FormInstance>()
  const checked = ref(false)
  const ruleForm = ref<any>({
    date: null,
    siteId: null,
  })
  const siteIdList = ref([])
  const rules = reactive({
    date: [{ required: true, message: '请选择日期', trigger: 'blur' }],
  })
  const dialogVisible = ref(false)
  const baseInfo = ref<any>({})
  const column = [
    { key: 'number', label: '插座数量:', span: 12, icon: 'Key' },
    { key: 'price', label: '电价:', span: 12, icon: 'Money', unit: '元/千瓦时' },
    { key: 'feeRate', label: '电费分成比例:', span: 12, icon: 'PieChart', unit: '%' },
    { key: 'incomeRate', label: '收入分成比例:', span: 12, icon: 'PieChart', unit: '%' },
    { key: 'type', label: '抄表类型:', span: 12, icon: 'CollectionTag' },
  ]
  //新增或修改
  const submit = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.validate(async (valid) => {
      if (valid) {
        ifWaiting(() => {
          let a = {
            startYear: ruleForm.value.date[0].split('-')[0] * 1,
            startMonth: ruleForm.value.date[0].split('-')[1] * 1,
            endYear: ruleForm.value.date[1].split('-')[0] * 1,
            endMonth: ruleForm.value.date[1].split('-')[1] * 1,
          }
          if (!checked.value) {
            a.siteId = ruleForm.value.siteId
          }
          postSiteDetailCalculate(a).then(async (result) => {
            if (result.code === 200) {
              dialogVisible.value = false
              await emit('getData')
              ElMessage.success('更新成功')
            }
          })
        })
      } else {
        ElMessage.warning('请正确填写表单')
        return false
      }
    })
  }

  const test = async () => {
    ifWaiting(() => {
      console.log(123)
    })
  }

  const show = () => {
    ruleForm.value.date = null
    postSiteMatch({}).then((res) => {
      siteIdList.value = res.data
      ruleForm.value.siteId = null
    })
    dialogVisible.value = true
    nextTick(() => {
      ruleFormRef.value.clearValidate()
    })
  }
  const getSiteInfo = async () => {
    let res = await getSiteById({ id: ruleForm.value.siteId })
    if (res.code === 200) {
      baseInfo.value = res.data
    }
  }
  const emit = defineEmits(['getData'])
  defineExpose({
    show,
  })
</script>

<style lang="scss" scoped>
  .col-content {
    display: flex;
    height: 100%;
    .label-info {
      display: flex;
      align-items: center;
      justify-content: end;
      color: #606266;
      font-weight: bold;
      font-size: 14px;
      background-color: #f5f7fa;
      min-width: 110px;
      border: 1px solid #ebeef5;
      padding: 0 10px;
      .icons {
        margin-right: 6px;
        height: 16px;
        width: 16px;
      }
    }
    .content-info {
      display: flex;
      align-items: center;
      width: 100%;
      padding: 10px;
      border: 1px solid #ebeef5;
    }
  }
</style>
